package io.sousou.handle;


import io.sousou.utils.E;
import io.sousou.utils.R;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author allen
 * @website suosuo.io
 * 2017-01-21 13:59
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public R handle(Exception e) {
        if (e instanceof E) {
            E ex = (E) e;
            logger.info(ex.getMessage());
            return R.error(ex.getCode(), ex.getMessage());
        }else {
            logger.error("【系统异常】{}", e);
            return R.unknow_error();
        }
    }
}
